package com.news.dao.impl;


import com.news.dao.UmsUserDao;
import com.news.entity.Admin;
import com.news.entity.User;
import com.news.utils.JdbcUtils;
import com.news.utils.PassWordEncoder;
import java.sql.*;


/**
 * @Author:weiming
 * @Date:2021/3/29
 */
public class UmsUserDaoImpl implements UmsUserDao {

    Connection connection = JdbcUtils.getConnection();

    @Override
    public int login(Admin admin) {
        String query = "SELECT id,password FROM ums_admin where username = ?";
        int result = 0;
        try {
            PreparedStatement pst = connection.prepareStatement(query);
            pst.setString(1, admin.getUserName());
            ResultSet resultSet = pst.executeQuery();
            int size = 0;
            String rexPwd = null;
            int id = 0;
            while(resultSet.next()){
                id = resultSet.getInt(1);
                rexPwd = resultSet.getString(2);
                size++;
            }
            if(size == 0){
                result =  0;
            }else if(PassWordEncoder.match(rexPwd,admin.getPassWord())){
                result = id;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return result;
    }


    @Override
    public int regist(User user) {
        String query = "SELECT * FROM ums_user where username = ?";
        int result  = 0;
        try {
            PreparedStatement pst = connection.prepareStatement(query);
            pst.setString(1, user.getUserName());
            ResultSet resultSet = pst.executeQuery();
            int size = 0;
            while(resultSet.next()){
                size++;
            }
            if (size > 0){
                return 0;
            }
            resultSet.close();
            pst.close();
            String  insert = "INSERT INTO ums_user (username,password) VALUES (? ,?)";
            PreparedStatement  pst2 = connection.prepareStatement(insert);
            pst2.setString(1, user.getUserName());
            pst2.setString(2, PassWordEncoder.encode(user.getPassWord()));
            result = pst2.executeUpdate();
            pst2.close();
            connection.commit();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return result;
    }

    @Override
    public int userLogin(User user) {
        String query = "SELECT id,password FROM ums_user where username = ?";
        int result = 0;
        try {
            PreparedStatement pst = connection.prepareStatement(query);
            pst.setString(1, user.getUserName());
            ResultSet resultSet = pst.executeQuery();
            int size = 0;
            String rexPwd = null;
            int id = 0;
            while(resultSet.next()){
                id = resultSet.getInt(1);
                rexPwd = resultSet.getString(2);
                size++;
            }
            if(size == 0){
                result =  0;
            }else if(PassWordEncoder.match(rexPwd,user.getPassWord())){
                result = id;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return result;
    }

    @Override
    public int checkUsername(String userName) {
        String query = "SELECT * FROM ums_user where username = ?";
        int flag = 1;
        PreparedStatement pst = null;
        ResultSet resultSet = null;
        try {
            pst = connection.prepareStatement(query);
            pst.setString(1, userName);
            resultSet = pst.executeQuery();
            int size = 0;
            while(resultSet.next()){
                size++;
            }
            if (size > 0){
                flag = 0;
            }
            pst.close();
            resultSet.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return  flag;
    }
}
